package com.acme.bankapp.domain.bank.account;

import com.acme.bankapp.domain.bank.exception.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {

    private int balance;

    public AbstractAccount(int startBalance) {
        if (startBalance < 0)
            throw new IllegalArgumentException("Balance should be more than zero");
        this.balance = startBalance;
    }

    @Override
    public int getMoney() {
        return balance;
    }

    @Override
    public Account depositMoney(int amount) {
        validateMoney(amount);
        addMoney(amount);
        return this;
    }

    @Override
    public Account withdrawMoney(int amount) throws NotEnoughFundsException {
        validateMoney(amount);
        checkToSubtractMoney(amount);
        subtractMoney(amount);
        return this;
    }

    protected void addMoney(int money) {
        balance += money;
    }

    protected void checkToSubtractMoney(int money) throws NotEnoughFundsException {
        if (balance < money)
            throw new NotEnoughFundsException();
    }

    protected void subtractMoney(int money) {
        balance -= money;
    }

    protected void validateMoney(int amount) {
        if (amount < 0)
            throw new IllegalArgumentException("Money must be positive");
    }
}
